package com.base.cn.platform.os.controller.web.course.play.exercises;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.cou.play.PlayCourseService;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import com.base.cn.platform.os.service.exam.record.LoadStartExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 随堂练习Controller接口
 *
 * @author s.li
 * @create 2018-08-02-18:43
 */
@Controller
@RequestMapping("/web/uc/play/exercises")
public class WebPlayCourseExercisesController extends WebBaseController {

    @Autowired
    private PlayCourseService playCourseService;
    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private LoadStartExamService loadStartExamService;

    /**
     * 验证随堂练习
     * @param courseId 购买的课程或套餐ID
     * @param catalogId 目录ID
     * @return Map<String,Object>
     */
    @RequestMapping("/checkExercises")
    @ResponseBody
    public Map<String,Object> checkExercises(HttpServletRequest request,
                                             @RequestParam("courseId") BigDecimal courseId,
                                             @RequestParam("catalogId") BigDecimal catalogId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return playCourseService.checkExercises(userId,courseId,catalogId);
    }

    /**
     * 加载随堂练习
     * @return
     */
    @RequestMapping("/loadExercises")
    public String loadExercises(HttpServletRequest request,
                                Model model,
                                @RequestParam("examRecordId") BigDecimal examRecordId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> condition = new HashMap<>();
        condition.put("id",examRecordId);
        condition.put("userId",userId);
        Map<String,Object> dataMap = loadStartExamService.loadExamRecord(condition);
        if(ResultUtil.getSuccess(dataMap)){
            model.addAttribute("examRecord",ResultUtil.getEntity(dataMap));
        }
        return resultPage("/course/play/exercises/course-exercises");
    }


    /**
     * 保存随堂练习
     * @param request HttpServletRequest
     * @param courseId 购买课程ID
     * @param catalogId 目录ID
     * @param examRecordId 随堂练习记录ID
     * @return Map<String, Object>
     */
    @RequestMapping("/saveExercises")
    @ResponseBody
    public Map<String, Object> saveExercises(HttpServletRequest request,
                                             @RequestParam("courseId") BigDecimal courseId,
                                             @RequestParam("catalogId") BigDecimal catalogId,
                                             @RequestParam("examRecordId") BigDecimal examRecordId) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return coursePlayRecordService.saveExercises(examRecordId,courseId,catalogId,userId);
    }

    //================================================================================================

    /**
     * 加载随堂练习试题
     * @param request HttpServletRequest
     * @param model Model
     * @param examRecordId 考试记录ID
     * @param status 状态，1未完成，2已完成
     * @param currentPage 页码
     * @return 数据列表
     */
    @RequestMapping("/loadExercisesQuestionList")
    public String loadExercisesQuestionList(HttpServletRequest request,
                                       Model model,
                                       @RequestParam("examRecordId") BigDecimal examRecordId,
                                       @RequestParam("status") int status,
                                       @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> params = new HashMap<>();
        params.put("type",1);
        params.put("userId",userId);
        params.put("examRecordId",examRecordId);
        params.put("questionData",true);
        params.put("templateData",true);
        Map<String,Object> dataMap = loadStartExamService.loadExamQuestionRecord(params,currentPage,10);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("status",status);
        return resultPage("/course/play/exercises/exerciser-question-list");
    }

    /**
     * 加载随堂练习的答题卡
     * @param request HttpServletRequest
     * @param model model
     * @param examRecordId 考试记录ID
     * @return 列表页面
     */
    @RequestMapping("/loadExercisesCard")
    public String loadExercisesCard(HttpServletRequest request,
                           Model model,
                           @RequestParam("examRecordId") BigDecimal examRecordId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> cardMap = loadStartExamService.loadExamRecordCard(userId,examRecordId,10);
        model.addAttribute("cardMap",cardMap);
        return resultPage("/course/play/exercises/exerciser-card");
    }
}
